package com.kcsoft.controller;

import com.kcsoft.pojo.Message;
import com.kcsoft.pojo.PageBean;
import com.kcsoft.pojo.Result;
import com.kcsoft.pojo.Sessions;
import com.kcsoft.service.MessageService;
import com.kcsoft.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/msg")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/sessions")
    public Result<List<Sessions>> getSessions(String assistantId){
        List<Sessions> slist = messageService.findByUser(assistantId);
        return Result.success(slist);
    }

    @PostMapping("/save")
    public Result saveMsg(@RequestBody List<Message> messages) {
        messageService.saveMsg(messages);
        return Result.success();
    }

    @PostMapping ("/list")
    public Result<PageBean<Message>> list(Integer pageNum, Integer pageSize, String sessionId) {

        PageBean<Message> pb = messageService.list(pageNum, pageSize, sessionId);

        return Result.success(pb);

    }

    @PostMapping("/listAll")
    public Result<PageBean<Message>> list(Integer pageNum, Integer pageSize) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer) claims.get("id");
        PageBean<Message> pb = messageService.listAll(pageNum, pageSize, id);
        return Result.success(pb);
    }
}
